package com.yanqu.road.utils;

import com.yanqu.road.utils.date.DateHelper;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.temporal.ChronoField;
import java.util.TimeZone;

public class LocalDateTimeHelper {

	public static long getZeroTimeTimeStamp() {
		LocalDateTime nowTime = LocalDateTime.now();
		LocalDateTime zeroDate = LocalDateTime.of(nowTime.getYear(), nowTime.getMonth(), nowTime.getDayOfMonth(), 0, 0,
				0);
		long zeroTime = zeroDate.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();

		return zeroTime;
	}

	/**
	 * 获取某个时间戳 当天凌晨 的时间戳
	 * @param timeMillis
	 * @return
	 */
	public static long getZeroTimeTimeStamp(long timeMillis) {
		LocalDateTime time = LocalDateTime.ofEpochSecond(timeMillis/1000,0, ZoneOffset.ofHours(getDefaultTimeZone()));
		LocalDateTime zeroDate = LocalDateTime.of(time.getYear(), time.getMonth(), time.getDayOfMonth(), 0, 0,
				0);
		long zeroTime = zeroDate.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();

		return zeroTime;
	}


	public static boolean isSameWeek(LocalDateTime time) {
		LocalDateTime today = LocalDateTime.now();
		return today.get(ChronoField.ALIGNED_WEEK_OF_YEAR) == time.get(ChronoField.ALIGNED_WEEK_OF_YEAR);
	}

	public static int getDefaultTimeZone() {
		return TimeZone.getDefault().getRawOffset() / 3600000;
	}

	/**
	 * 转化时区
	 *
	 * @param localTime               本地时区时间，如日本0点（日本rs.getTimestamp读港澳台库的0点，会变成日本的0点）
	 * @param channelActivityTimeZone 目标时区，如东八区
	 * @param isSecond                单位是否为秒
	 * @return 其他地区时间，如日本0点会变成东八区0点
	 */
	public static long convertTimeZone(long localTime, int channelActivityTimeZone, boolean isSecond) {
		if (channelActivityTimeZone < -12 || channelActivityTimeZone > 14) {
			return localTime;
		}

		TimeZone b = TimeZone.getDefault();
		long offset = b.getRawOffset() - channelActivityTimeZone * DateHelper.HOUR_MILLIONS;

		if (isSecond) {
			offset = offset / 1000;
		}

		return localTime + offset;
	}

	public static void main(String[] args) {
		LocalDateTime yy = LocalDateTime.of(2020, 6, 25, 0, 0, 0);
		System.out.println(isSameWeek(yy));
	}
}
